//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace NopSolutions.NopCommerce.DataAccess.Products.Specs
{
    /// <summary>
    /// Specification attribute provider for SQL Server
    /// </summary>
    public class SQLSpecificationAttributeProvider : DBSpecificationAttributeProvider
    {
        #region Fields
        private string _sqlConnectionString;
        #endregion

        #region Utilities
        private DBSpecificationAttribute GetSpecificationAttributeFromReader(IDataReader dataReader)
        {
            DBSpecificationAttribute specificationAttribute = new DBSpecificationAttribute();
            specificationAttribute.SpecificationAttributeID = NopSqlDataHelper.GetInt(dataReader, "SpecificationAttributeID");
            specificationAttribute.Name = NopSqlDataHelper.GetString(dataReader, "Name");
            return specificationAttribute;
        }

        private DBProductSpecificationAttribute GetProductSpecificationAttributeFromReader(IDataReader dataReader)
        {
            DBProductSpecificationAttribute productSpecificationAttribute = new DBProductSpecificationAttribute();
            productSpecificationAttribute.ProductSpecificationAttributeID = NopSqlDataHelper.GetInt(dataReader, "ProductSpecificationAttributeID");
            productSpecificationAttribute.ProductID = NopSqlDataHelper.GetInt(dataReader, "ProductID");
            productSpecificationAttribute.SpecificationAttributeID = NopSqlDataHelper.GetInt(dataReader, "SpecificationAttributeID");
            productSpecificationAttribute.AttributeValue = NopSqlDataHelper.GetString(dataReader, "AttributeValue");
            productSpecificationAttribute.DisplayOrder = NopSqlDataHelper.GetInt(dataReader, "DisplayOrder");
            return productSpecificationAttribute;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Initializes the provider with the property values specified in the application's configuration file. This method is not intended to be used directly from your code
        /// </summary>
        /// <param name="name">The name of the provider instance to initialize</param>
        /// <param name="config">A NameValueCollection that contains the names and values of configuration options for the provider.</param>
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            base.Initialize(name, config);

            string connectionStringName = config["connectionStringName"];
            if (String.IsNullOrEmpty(connectionStringName))
                throw new ProviderException("Connection name not specified");
            this._sqlConnectionString = NopSqlDataHelper.GetConnectionString(connectionStringName);
            if ((this._sqlConnectionString == null) || (this._sqlConnectionString.Length < 1))
            {
                throw new ProviderException(string.Format("Connection string not found. {0}", connectionStringName));
            }
            config.Remove("connectionStringName");

            if (config.Count > 0)
            {
                string key = config.GetKey(0);
                if (!string.IsNullOrEmpty(key))
                {
                    throw new ProviderException(string.Format("Provider unrecognized attribute. {0}", new object[] { key }));
                }
            }
        }

        /// <summary>
        /// Gets a specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        /// <returns>Specification attribute</returns>
        public override DBSpecificationAttribute GetSpecificationAttributeByID(int SpecificationAttributeID)
        {
            DBSpecificationAttribute specificationAttribute = null;
            if (SpecificationAttributeID == 0)
                return specificationAttribute;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_SpecificationAttributeLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, SpecificationAttributeID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    specificationAttribute = GetSpecificationAttributeFromReader(dataReader);
                }
            }
            return specificationAttribute;
        }

        /// <summary>
        /// Gets specification attribute collection
        /// </summary>
        /// <returns>Specification attribute collection</returns>
        public override DBSpecificationAttributeCollection GetSpecificationAttributes()
        {
            DBSpecificationAttributeCollection specificationAttributeCollection = new DBSpecificationAttributeCollection();
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_SpecificationAttributeLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    DBSpecificationAttribute specificationAttribute = GetSpecificationAttributeFromReader(dataReader);
                    specificationAttributeCollection.Add(specificationAttribute);
                }
            }

            return specificationAttributeCollection;
        }

        /// <summary>
        /// Deletes a specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        public override void DeleteSpecificationAttribute(int SpecificationAttributeID)
        {
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_SpecificationAttributeDelete");
            db.AddInParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, SpecificationAttributeID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Inserts a specification attribute
        /// </summary>
        /// <param name="Name">The name</param>
        /// <returns>Specification attribute</returns>
        public override DBSpecificationAttribute InsertSpecificationAttribute(string Name)
        {
            DBSpecificationAttribute specificationAttribute = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_SpecificationAttributeInsert");
            db.AddOutParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int SpecificationAttributeID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@SpecificationAttributeID"));
                specificationAttribute = GetSpecificationAttributeByID(SpecificationAttributeID);
            }
            return specificationAttribute;
        }

        /// <summary>
        /// Updates the specification attribute
        /// </summary>
        /// <param name="SpecificationAttributeID">The specification attribute identifier</param>
        /// <param name="Name">The name</param>
        /// <returns>Specification attribute</returns>
        public override DBSpecificationAttribute UpdateSpecificationAttribute(int SpecificationAttributeID, string Name)
        {
            DBSpecificationAttribute specificationAttribute = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_SpecificationAttributeUpdate");
            db.AddInParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, SpecificationAttributeID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                specificationAttribute = GetSpecificationAttributeByID(SpecificationAttributeID);

            return specificationAttribute;
        }

        /// <summary>
        /// Deletes a product specification attribute mapping
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">Product specification attribute identifier</param>
        public override void DeleteProductSpecificationAttribute(int ProductSpecificationAttributeID)
        {
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Product_SpecificationAttribute_MappingDelete");
            db.AddInParameter(dbCommand, "ProductSpecificationAttributeID", DbType.Int32, ProductSpecificationAttributeID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Gets a product specification attribute mapping collection
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product specification attribute mapping collection</returns>
        public override DBProductSpecificationAttributeCollection GetProductSpecificationAttributesByProductID(int ProductID)
        {
            DBProductSpecificationAttributeCollection productSpecificationAttributes = new DBProductSpecificationAttributeCollection();
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Product_SpecificationAttribute_MappingLoadByProductID");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    DBProductSpecificationAttribute productSpecificationAttribute = GetProductSpecificationAttributeFromReader(dataReader);
                    productSpecificationAttributes.Add(productSpecificationAttribute);
                }
            }

            return productSpecificationAttributes;
        }

        /// <summary>
        /// Gets a product specification attribute mapping 
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">Product specification attribute mapping identifier</param>
        /// <returns>Product specification attribute mapping</returns>
        public override DBProductSpecificationAttribute GetProductSpecificationAttributeByID(int ProductSpecificationAttributeID)
        {
            DBProductSpecificationAttribute productSpecificationAttribute = null;
            if (ProductSpecificationAttributeID == 0)
                return productSpecificationAttribute;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Product_SpecificationAttribute_MappingLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ProductSpecificationAttributeID", DbType.Int32, ProductSpecificationAttributeID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    productSpecificationAttribute = GetProductSpecificationAttributeFromReader(dataReader);
                }
            }
            return productSpecificationAttribute;
        }

        /// <summary>
        /// Inserts a product specification attribute mapping
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="SpecificationAttributeID">Specification attribute identifier</param>
        /// <param name="AttributeValue">An attribute value</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product specification attribute mapping</returns>
        public override DBProductSpecificationAttribute InsertProductSpecificationAttribute(int ProductID, int SpecificationAttributeID,
                 string AttributeValue, int DisplayOrder)
        {
            DBProductSpecificationAttribute productSpecificationAttribute = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Product_SpecificationAttribute_MappingInsert");
            db.AddOutParameter(dbCommand, "ProductSpecificationAttributeID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, SpecificationAttributeID);
            db.AddInParameter(dbCommand, "AttributeValue", DbType.String, AttributeValue);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int ProductSpecificationAttributeID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ProductSpecificationAttributeID"));
                productSpecificationAttribute = GetProductSpecificationAttributeByID(ProductSpecificationAttributeID);
            }
            return productSpecificationAttribute;
        }

        /// <summary>
        /// Updates the product specification attribute mapping
        /// </summary>
        /// <param name="ProductSpecificationAttributeID">product specification attribute mapping identifier</param>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="SpecificationAttributeID">Specification attribute identifier</param>
        /// <param name="AttributeValue">An attribute value</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product specification attribute mapping</returns>
        public override DBProductSpecificationAttribute UpdateProductSpecificationAttribute(int ProductSpecificationAttributeID,
               int ProductID, int SpecificationAttributeID, string AttributeValue, int DisplayOrder)
        {
            DBProductSpecificationAttribute productSpecificationAttribute = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_Product_SpecificationAttribute_MappingUpdate");
            db.AddInParameter(dbCommand, "ProductSpecificationAttributeID", DbType.Int32, ProductSpecificationAttributeID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "SpecificationAttributeID", DbType.Int32, SpecificationAttributeID);
            db.AddInParameter(dbCommand, "AttributeValue", DbType.String, AttributeValue);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                productSpecificationAttribute = GetProductSpecificationAttributeByID(ProductSpecificationAttributeID);

            return productSpecificationAttribute;
        }
       
        #endregion
    }
}
